<?php
namespace Admin\Controller;
use Org\Util\Page;
class ProductController extends CommonController {
    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('product');
        $this->_cate_mod = D('ProductCate');
    }

    public function _before_index() {
        //显示模式
        $sm = I('request.sm');
        $this->assign('sm', $sm);
        //分类信息
        $res = $this->_cate_mod->field('id,name')->select();
        $cate_list = array();
        foreach ($res as $val) {
            $cate_list[$val['id']] = $val['name'];
        }
        $this->assign('cate_list', $cate_list);

    }
    protected function _search(){
        $map = array();
        //'status'=>1
        ($time_start = I('get.time_start')) && $map['add_time'][] = array('egt', strtotime($time_start));
        ($time_end = I('get.time_end')) && $map['add_time'][] = array('elt', strtotime($time_end)+(24*60*60-1));

        $cate_id = I('get.cate_id');
        $selected_ids = '';
        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
            $spid = $this->_cate_mod->where(array('id'=>$cate_id))->getField('spid');
            $selected_ids = $spid ? $spid . $cate_id : $cate_id;
        }
        if( $_GET['status']==null ){
            $status = -1;
        }else{
            $status = intval($_GET['status']);
        }
        $status>=0 && $map['status'] = array('eq',$status);
        ($keyword = I('get.keyword')) && $map['title'] = array('like', '%'.$keyword.'%');
        $this->assign('search', array(
            'time_start' => $time_start,
            'time_end' => $time_end,
            'status' =>$status,
            'cate_id' => $cate_id,
            'selected_ids' => $selected_ids,
            'keyword' => $keyword,
        ));
		//var_dump($keyword);
        return $map;
    }

    public function _before_add(){
        $this->unit = getUnit();
    }

    protected function _before_insert($data) {
        $data['add_time']=time();
        $data['img_all']=serialize($data['img']);
        $data['img']=$data['img']['y'];
        if(!isset($data['cuxiao']) && $data['cuxiao']!=1){
            $data['c_price'] = 0;
            $data['c_memo']  = '';
            $data['cuxiao']  = 0;
        }
        $data['relation'] = trim($data['relation'],',');
        return $data;

    }
    public function selectgoods(){
        $response = $this->fetch();
        $this->ajaxReturn(1,'',$response);
}
    public function filtergoods(){
        $pagesize = 18;
        $cateid = I('get.cat_id',0);
        $q      = I('get.q');
        $page   = I('get.page');
        $where  = array();
        if($cateid){
            $where['cat_id'] = $cateid;
        }
        if($q){
            $where['title'] = array('like','%'.$q.'%');
        }
       // $productObj = M('product');
        //分页类
        $count   = $this->_mod->where($where)->count();
        $pageObj = new Page($count,$pagesize);
        $result = $this->_mod->where($where)->field('id,img,title')->page($page,$pageObj->listRows)->select();
        $res    = array('page'=>$pageObj->ajaxPage($page),'data'=>$result);
        $this->ajaxReturn(1,'',$res);
    }
    public function _before_edit(){
        $id = I('id');
        $product = $this->_mod->field('id,cate_id')->where(array('id'=>$id))->find();
        $spid = $this->_cate_mod->where(array('id'=>$product['cate_id']))->getField('spid');
        if( $spid==0 ){
            $spid = $product['cate_id'];
        }else{
            $spid .= $product['cate_id'];
        }
        $this->unit = getUnit();
        $this->assign('selected_ids',$spid);
    }

    protected function _before_update($data) {
        $data['img_all']=serialize($data['img']);
        $data['img']=$data['img']['y'];
        if(!isset($data['cuxiao']) && $data['cuxiao']!=1){
            $data['c_price'] = 0;
            $data['c_memo']  = '';
            $data['cuxiao']  = 0;
        }
        $data['relation'] = trim($data['relation'],',');
        return $data;
    }

    //无刷新上传图片
    public function ajax_upload_img() {
        if (!empty($_FILES['img']['name'])) {
            $dir = date('ymd/');
            $result = $this->_upload($_FILES['img'], './Uploads/product/'. $dir );
            if ($result['error']) {
                $this->ajaxReturn(0, $result['info']);
            } else {
                $savename = $dir . $result['info'][0]['savename'];
                $this->ajaxReturn(1, L('operation_success'), $savename);
            }
        } else {
            $this->ajaxReturn(0, L('illegal_parameters'));
        }
    }

}